package com.hzzc.core.controller;

import com.hzzc.BizException;
import com.hzzc.Result;
import com.hzzc.api.*;
import com.hzzc.core.service.PasswdEncoder;
import com.hzzc.core.service.sdk.ApiUserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户登录授权接口
 */
@Slf4j
@Controller
@RequestMapping("/sso")
public class AuthController {

    @Autowired
    private PasswdEncoder passwdEncoder;

    @Autowired
    private ApiUserLoginService userLoginService;

    /**
     * 账户密码登录
     *
     * @param loginBean
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/login/account")
    public Result<SSOLoginModel> loginByAccountToReset(@RequestBody LoginBean loginBean) throws BizException {
        return Result.success(userLoginService.login(loginBean), "处理登录授权成功！");
    }

    /**
     * 账户密码登录
     *
     * @param loginBean
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/login/encryption")
    public Result<SSOLoginModel> loginByEncryptionToReset(@RequestBody LoginBean loginBean) throws BizException {
        loginBean.setPass(passwdEncoder.encode(loginBean.getPass()));
        return Result.success(userLoginService.login(loginBean), "处理登录授权成功！");
    }

    /**
     * 检测登录状态
     *
     * @param checkBean
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/login/check")
    public Result<String> checkAuthority(@RequestBody CheckBean checkBean) throws BizException {
        return userLoginService.checkAuthority(checkBean);
    }

    /**
     * 检测登录状态
     *
     * @param authorityBean
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/login/authorityView")
    public Result<SSOApplicationUserAuthorityView> authorityView(@RequestBody AuthorityBean authorityBean) throws BizException {
        return userLoginService.getUserApplicationUserAuthorityView(authorityBean.getApp(), authorityBean.getCode());
    }

    /**
     * 登出
     *
     * @param logoutBean
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Result<Boolean> loginByAccountToReset(@RequestBody LogoutBean logoutBean) throws BizException {
        return Result.success(userLoginService.logout(logoutBean), "退出登录成功！");
    }

    /**
     * 检测登录状态
     *
     * @param to
     * @return
     * @throws BizException
     */
    @RequestMapping("/page")
    public ModelAndView loginPage(@RequestParam("to") String to, ModelAndView modelAndView) throws Exception {
        modelAndView.addObject("to", to);
        modelAndView.setViewName("login");
        return modelAndView;
    }


}
